<?php
namespace app\admin\controller;
use think\Db;

class Goodscat extends Base
{   
    public function lists(){
        //ajax查询数据
        if( is_post() ){
            $page     = input('page');
            $cat_id   = input('cat_id');
            $status   = input('status');
            $keyword  = input('keyword');
            
            //原生态sql查询
            $sql = "
                select
                    [1]
                from
                    goods_cat a
                left join
                    goods_cat b
                on
                    a.parent_id = b.id
                where 
                    1 = ?
                ";
            $param = [1];
            if ( $cat_id != '100all' ) {
                $sql .= " and a.parent_id = ?";
                $param[] = $cat_id;
            }
            if ( $status != '100all' ) {
                $sql .= " and a.status = ?";
                $param[] = $status;
            }
            if ( !empty($keyword) ) {
                $sql .= " and a.cat_title like ? ";
                $param[] = "%{$keyword}%";
            }
            //总条数
            $sql_count = str_replace("[1]", "count(*) as total", $sql);
            $total     = Db::query($sql_count, $param);
            $total     = $total[0]['total'];
            
            //分页数据
            $page_size   = c('page_size');     #分页条数
            $page        = com_cur_page($total, $page,  $page_size);   #当前页
			$page_data   = com_page_data($total, $page,  $page_size);   #分页原始数据
            
            //列表数据
			$start       = ($page - 1) * $page_size;
			$sql        .= " order by a.sort desc, a.id desc limit $start, $page_size";
			$field       = "a.*, b.cat_title as parent_title";
			$sql_list    = str_replace("[1]", $field, $sql);
            $list        = Db::query($sql_list, $param);
            
            //格式数据
            foreach($list as $k => $v){    
                $list[$k]['checkbox']       =  com_format_checkbox($v['id']);
                $list[$k]['create_time']    =  com_format_time( $v['create_time'] );
                $list[$k]['sort']           =  com_format_input_edit(url('ajax/editSort', 't=goods_cat'), $v['id'], $v['sort']);
                //标题
                $tag       = "";
                $count     = count(explode(",", $v['cat_tree']));
                for($i = 1; $i<$count; $i++){
                    $tag .= "<span class='ml-10'>&nbsp;</span>";
                }
                $list[$k]['cat_title']      =   $tag."-|".$v['cat_title'];
                //$list[$k]['status']         =  com_format_select_edit( $v['status'], c('type.status'), url('ajax/editStatus', 't=goods_cat'), $v['id'] );
                $list[$k]['status']         =com_format_status2(url('ajax/editStatus', 't=goods_cat'),$v['id'],$v['status']);
                //操作按钮
                $list[$k]['action']         =  com_action_edit( url("goodscat/edit","id={$v['id']}"), $v['id'], '编辑分类' );
                $list[$k]['action']        .=  com_action_del( url("goodscat/delete"), $v['id'], "你确定要删除{$v['cat_title']}吗？" );
            }
			$com_body    = com_build_table($list, array("checkbox", "id",  "cat_title", "parent_title", "sort", "status", "create_time", "action"));
			$com_page    = com_build_page( $page_data );
			$data = array(
				"com_body"  => $com_body,
				"com_page"  => $com_page,
			);
			
			$config = [
			    'data'   => $data,
			    'suc'    => [
			        'js_action' => 'function.switchs',
			    ],
			];
			com_check($config);
        }

        $all= [
            '100all' => [
                'id' => '100all',
                'parent_id' => 0,
                'cat_title' => '全部分类',
                'depath' => 0,
                ]
        ];
        $category = $all + \app\admin\model\GoodsCat::allSubCat();
        $data = [
            'nav'       => ['商品分类', '列表'],
            'category'  => $category,
            'status'    => ['100all'=>'全部状态'] + c('type.status'),
        ];
        return view('', $data);
    }
    //添加
    public function add(){
        if( is_post() ){
            $data = input();
            //验证
            $rule = [
                'cat_title'  =>  'require',
                'sort'       =>  'require',
            ];
            $msg = [
                'cat_title.require'  =>  '标题不能为空',
                'sort.require'       =>  '排序值不能为空',
            ];
            com_validate( $rule, $msg, $data, true );
            
            $data['create_time']=time();
            $data['update_time']=time();
            
            //树节点
            $data['cat_tree'] = \app\admin\model\GoodsCat::buildTree($data['parent_id']);
            $ac = new \app\admin\model\GoodsCat();
            $ac->save($data);
            $config = [
                'suc' => [
                    'msg' => '添加分类成功！',
                    'js_action' => 'alert|close',
                ],
            ];
            com_check( $config );
        }


        $all= [
            '100all' => [
                'id' => 0,
                'parent_id' => 0,
                'cat_title' => '顶级分类',
                'depath' => 0,
                ]
        ];
        $category = $all + \app\admin\model\GoodsCat::allSubCat();
        $data = [
            'category'  => $category,
            'status'    => c('type.status'),
        ];
        return view('', $data);
    }
    
    //编辑
    public function edit( $id ){
        if( is_post() ){
            $data = input();  #post数据
            //验证
            $rule = [
                   'cat_title'      =>  'require',
                   'sort'           =>  'require',
                ];
            $msg = [
                   'cat_title.require'      =>  '标题不能为空',
                   'sort.require'           =>  '排序值不能为空',
                ];
            com_validate( $rule, $msg, $data, true );
            
            $data['update_time']=time();
            
            //树节点
            $data['cat_tree'] = \app\admin\model\GoodsCat::buildTree($data['parent_id']);
            $data['id']       = $id;
            \app\admin\model\GoodsCat::update($data);
            $config = [
                'suc' => [
                    'msg' => '编辑分类成功！',
                    'js_action' => 'alert|close',
                ],
            ];
            com_check( $config );
        }
        
        //模板赋值
        $all= [
            '100all' => [
                'id'        => 0,
                'parent_id' => 0,
                'cat_tree'  => ",0,",
                'cat_title' => '顶级分类',
                'depath'    => 0,
            ]
        ];
        $category = $all + \app\admin\model\GoodsCat::allSubCat();
        $info     = \app\admin\model\GoodsCat::get($id)->getData();
        //干掉自身及以下的分类
        $cat_tree = $info['cat_tree'].$info['id'].",";
        foreach($category as $k => $v){
            if( strpos($v['cat_tree'], $cat_tree) === 0 || $v['id']==$info['id']){
                unset($category[$k]);
            }
        }
        $data = [
            'category'  => $category,
            'status'    => c('type.status'),
            'info'      => $info,
        ];
        return view('', $data);
    }
    
    //删除
    public function delete(){
        if( is_post() ){
            $id  =  input('id');
            $ac  =  new \app\admin\model\GoodsCat();
            if( $ac->checkDel( $id ) ){
                $ac::destroy($id);
            }
            
            $config = [
                'suc' => [
                    'msg'       => '删除成功!',
                    'js_action' => 'alert|reload',
                ],  
                'err' => [
                    'js_action' => 'alert',
                ]
            ];
            com_check($config);
        }
    }
    
    //批量删除
    public function delall(){
        if( is_post() ){
            $ids = input('ids/a');
            $ac  =  new \app\admin\model\GoodsCat();
            foreach($ids as $id){
                if( $ac->checkDel($id) ){
                       $ac::destroy($id);
                }
            }
            $config = [
                'err' => [
                    'js_action' => "alert|reload",
                ],
                'suc' => [
                    'msg' => '删除成功！',
                    'js_action'=>"alert|reload",
                ],
            ];
            com_check($config);
        }
    }










    public function taocan_lists(){
        //ajax查询数据
        if( is_post() ){
            $page           = input('page');
            $cat_id         = input('cat_id');
            $taocan_level   = input('taocan_level');
            $status         = input('status');
            $keyword        = input('keyword');
            
            //原生态sql查询
            $sql = "
                select
                    [1]
                from
                    goods_cat_taocan a
                left join
                    goods_cat_taocan b
                on
                    a.parent_id = b.id
                where 
                    1 = ?
                ";
            $param = [1];
            if ( $cat_id != '100all' ) {
                $sql .= " and a.parent_id = ?";
                $param[] = $cat_id;
            }
            if ( $taocan_level != '100all' ) {
                $sql .= " and a.taocan_level = ?";
                $param[] = $taocan_level;
            }
            if ( $status != '100all' ) {
                $sql .= " and a.status = ?";
                $param[] = $status;
            }
            if ( !empty($keyword) ) {
                $sql .= " and a.cat_title like ? ";
                $param[] = "%{$keyword}%";
            }
            //总条数
            $sql_count = str_replace("[1]", "count(*) as total", $sql);
            $total     = Db::query($sql_count, $param);
            $total     = $total[0]['total'];
            
            //分页数据
            $page_size   = c('page_size');     #分页条数
            $page        = com_cur_page($total, $page,  $page_size);   #当前页
            $page_data   = com_page_data($total, $page,  $page_size);   #分页原始数据
            
            //列表数据
            $start       = ($page - 1) * $page_size;
            $sql        .= " order by a.sort desc, a.id desc limit $start, $page_size";
            $field       = "a.*, b.cat_title as parent_title";
            $sql_list    = str_replace("[1]", $field, $sql);
            $list        = Db::query($sql_list, $param);
            
            //格式数据
            foreach($list as $k => $v){    
                $list[$k]['checkbox']       =  com_format_checkbox($v['id']);
                $list[$k]['create_time']    =  com_format_time( $v['create_time'] );
                $list[$k]['taocan_level']   =  c('type.taocan_level' , $v['taocan_level']);
                $list[$k]['sort']           =  com_format_input_edit(url('ajax/editSort', 't=goods_cat_taocan'), $v['id'], $v['sort']);
                //标题
                $tag       = "";
                $count     = count(explode(",", $v['cat_tree']));
                for($i = 1; $i<$count; $i++){
                    $tag .= "<span class='ml-10'>&nbsp;</span>";
                }
                $list[$k]['cat_title']    =   $tag."-|".$v['cat_title'];
                
                $list[$k]['status']=com_format_status(url('ajax/editStatus', 't=goods_cat_taocan'),$v['id'],$v['status']);
                //操作按钮
                $list[$k]['action']         =  com_action_edit( url("goodscat/taocan_edit","id={$v['id']}"), $v['id'], '编辑分类' );
                $list[$k]['action']        .=  com_action_del( url("goodscat/taocan_delete"), $v['id'], "你确定要删除{$v['cat_title']}吗？" );
            }
            $com_body    = com_build_table($list, array("checkbox", "id",  "cat_title", "parent_title", "taocan_level", "sort", "status", "create_time", "action"));
            $com_page    = com_build_page( $page_data );
            $data = array(
                "com_body"  => $com_body,
                "com_page"  => $com_page,
            );
            
            $config = [
                'data'   => $data,
                'suc'    => [
                    'js_action' => 'function.switchs',
                ],
            ];
            com_check($config);
        }

        $all= [
            '100all' => [
                'id' => '100all',
                'parent_id' => 0,
                'cat_title' => '全部分类',
                'depath' => 0,
                ]
        ];
        $category = $all + \app\admin\model\GoodsCatTaocan::allSubCat();
        $data = [
            'nav'           => ['套餐分类', '列表'],
            'category'      => $category,
            'taocan_level'    => ['100all'=>'全部状态'] + c('type.taocan_level'),
            'status'        => ['100all'=>'全部状态'] + c('type.status'),
        ];
        return view('', $data);
    }



    
    //添加
    public function taocan_add(){
        if( is_post() ){
            $data = input();
            //验证
            $rule = [
                'cat_title'  =>  'require',
                'sort'       =>  'require',
            ];
            $msg = [
                'cat_title.require'  =>  '标题不能为空',
                'sort.require'       =>  '排序值不能为空',
            ];
            com_validate( $rule, $msg, $data, true );
            
            $data['create_time']=time();
            $data['update_time']=time();
            
            //树节点
            $data['cat_tree'] = \app\admin\model\GoodsCatTaocan::buildTree($data['parent_id']);
            $ac = new \app\admin\model\GoodsCatTaocan();
            $ac->save($data);
            $config = [
                'suc' => [
                    'msg' => '添加分类成功！',
                    'js_action' => 'alert|close',
                ],
            ];
            com_check( $config );
        }

        $all= [
            '100all' => [
                'id' => 0,
                'parent_id' => 0,
                'cat_title' => '顶级分类',
                'depath' => 0,
                ]
        ];
        $category = $all + \app\admin\model\GoodsCatTaocan::allSubCat();
        $data = [
            'category'        => $category,
            'taocan_level'    => c('type.taocan_level'),
            'status'          => c('type.status'),
        ];
        return view('', $data);
    }
    
    //编辑
    public function taocan_edit( $id ){
        if( is_post() ){
            $data = input();  #post数据
            //验证
            $rule = [
                   'cat_title'      =>  'require',
                   'sort'           =>  'require',
                ];
            $msg = [
                   'cat_title.require'      =>  '标题不能为空',
                   'sort.require'           =>  '排序值不能为空',
                ];
            com_validate( $rule, $msg, $data, true );
            
            $data['update_time']=time();
            
            //树节点
            $data['cat_tree'] = \app\admin\model\GoodsCatTaocan::buildTree($data['parent_id']);
            $data['id']       = $id;
            \app\admin\model\GoodsCatTaocan::update($data);
            $config = [
                'suc' => [
                    'msg' => '编辑分类成功！',
                    'js_action' => 'alert|close',
                ],
            ];
            com_check( $config );
        }
        
        //模板赋值
        $all= [
            '100all' => [
                'id'        => 0,
                'parent_id' => 0,
                'cat_tree'  => ",0,",
                'cat_title' => '顶级分类',
                'depath'    => 0,
            ]
        ];
        $category = $all + \app\admin\model\GoodsCatTaocan::allSubCat();
        $info     = \app\admin\model\GoodsCatTaocan::get($id)->getData();
        //干掉自身及以下的分类
        $cat_tree = $info['cat_tree'].$info['id'].",";
        foreach($category as $k => $v){
            if( strpos($v['cat_tree'], $cat_tree) === 0 || $v['id']==$info['id']){
                unset($category[$k]);
            }
        }
        $data = [
            'category'  => $category,
            'status'    => c('type.status'),
            'taocan_level'    => c('type.taocan_level'),
            'info'      => $info,
        ];
        return view('', $data);
    }
    
    //删除
    public function taocan_delete(){
        if( is_post() ){
            $id  =  input('id');
            $ac  =  new \app\admin\model\GoodsCatTaocan();
            if( $ac->checkDel( $id ) ){
                $ac::destroy($id);
            }
            
            $config = [
                'suc' => [
                    'msg'       => '删除成功!',
                    'js_action' => 'alert|reload',
                ],  
                'err' => [
                    'js_action' => 'alert',
                ]
            ];
            com_check($config);
        }
    }
    
    //批量删除
    public function taocan_delall(){
        if( is_post() ){
            $ids = input('ids/a');
            $ac  =  new \app\admin\model\GoodsCatTaocan();
            foreach($ids as $id){
                if( $ac->checkDel($id) ){
                       $ac::destroy($id);
                }
            }
            $config = [
                'err' => [
                    'js_action' => "alert|reload",
                ],
                'suc' => [
                    'msg' => '删除成功！',
                    'js_action'=>"alert|reload",
                ],
            ];
            com_check($config);
        }
    }
    
    

    
}